package com.raul.prueba.biblioteca.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.raul.prueba.biblioteca.servlets.vo.VOUsuarios;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String usuario = request.getParameter("usuario");
		String password = request.getParameter("password");
		System.out.println("Usuario : "+usuario);
		System.out.println("Password : "+password);
		if(usuario.equals("Raul") && password.equals("1234")) {
			System.out.println("Welcome . . .");
			VOUsuarios vo = new VOUsuarios();
			vo.setUsuario(usuario);
			vo.setPassword(password);
			vo.setEdad(22);
			vo.setNombre("Raul Gutierrez");
			HttpSession session = request.getSession(true);
			List<VOUsuarios> lista = new ArrayList<VOUsuarios>();
			session.setAttribute("usuarioLogeado", vo);
			session.setAttribute("listaUsuario", lista);
			request.getRequestDispatcher("welcome.jsp").forward(request, response);
		} else {
			System.out.println("Error en credenciales de acceso . . .");
			response.sendRedirect("login.html");
		}
	}
}
